package org.fuse.prototype.common;

import java.io.File;
import java.lang.management.ManagementFactory;

import com.sun.tools.attach.VirtualMachine;

public class LogTest {
    public static void main(String[] args) throws Exception {

//        CtClass ctClass = ClassPool.getDefault().get("org.fuse.prototype.common.User");
//        String mname = "getProfile";
//        CtMethod mold = ctClass.getDeclaredMethod(mname);
//
//        String txt = "\nSystem.out.println(nameStr);\n";
//        mold.insertAt(8, txt);
//
//        mold.insertAt(9, "\nSystem.out.println(\"execute!....\");\n");
//
//        String txt2 = "\nSystem.out.println(nameStr2);\n";
//        mold.insertAt(11, txt2);
//
//        String txt3 = "\nSystem.out.println(nameStr3);\n";
//        mold.insertAt(15, true, txt3);
//
//        User user = (User)ctClass.toClass().newInstance();
//        user.getProfile(null);

         execute();
    }

    private static void execute() {
        String nameOfRunningVM = ManagementFactory.getRuntimeMXBean().getName();
        int p = nameOfRunningVM.indexOf('@');
        String pid = nameOfRunningVM.substring(0, p);
        System.out.println(pid);

        try {
//            String FOLDER_THAT_CONTAINS_ATTACH_DLL = "C:\\Program Files\\Java\\jre6\\bin";
//            System.setProperty("java.library.path", System.getProperty("java.library.path") + File.pathSeparator + FOLDER_THAT_CONTAINS_ATTACH_DLL);
            VirtualMachine vm = VirtualMachine.attach(pid);
            
            String jarPath = System.getProperty("user.dir") + "/target/fuse-framework-prototype.jar";
            vm.loadAgent(jarPath, "");
            vm.detach();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }

        User user = new User();
        user.getProfile(null);
    }
}
